﻿namespace IntellectShop.Common
{
	using System;
	using System.Collections.Generic;
	using System.Linq;

	public partial class PageContent
	{
		public static PageContent RetrieveByPageContentID(int pageContentID)
		{
			using (DB.IntellectShopDataContext dataContext = new DB.IntellectShopDataContext(Common.Globals.ConnectionString))
			{
				var query = (
							from pageContent in dataContext.PageContents
							where pageContent.PageContentID == pageContentID
							select pageContent).SingleOrDefault();

				Common.PageContent item = new Common.PageContent
				{
					PageContentID = query.PageContentID,
					PageContentUniqName = query.PageContentUniqName,
					Header = query.Header,
					Body = query.Body,
					CreatedBy = query.CreatedBy,
					Created = query.Created
				};

				return item;
			}
		}

		public static PageContent RetrieveByPageContentUniqName(string pageContentUniqName)
		{
			using (DB.IntellectShopDataContext dataContext = new DB.IntellectShopDataContext(Common.Globals.ConnectionString))
			{
				var query = (
							from pageContent in dataContext.PageContents
							where pageContent.PageContentUniqName == pageContentUniqName
							select pageContent).SingleOrDefault();

				if (query == null)
					return null;

				Common.PageContent item = new Common.PageContent
				{
					PageContentID = query.PageContentID,
					PageContentUniqName = query.PageContentUniqName,
					Header = query.Header,
					Body = query.Body,
					CreatedBy = query.CreatedBy,
					Created = query.Created
				};

				return item;
			}
		}

		public static List<Common.PageContent> RetrieveAll()
		{
			using (DB.IntellectShopDataContext context = new DB.IntellectShopDataContext(Globals.ConnectionString))
			{
				var query =
							from pageContent in context.PageContents
							orderby pageContent.Created, pageContent.PageContentID
							select pageContent;

				List<Common.PageContent> result = new List<Common.PageContent>();
				foreach (var row in query)
				{
					result.Add(new Common.PageContent
					{
						PageContentID = row.PageContentID,
						PageContentUniqName = row.PageContentUniqName,
						Header = row.Header,
						Body = row.Body,
						CreatedBy = row.CreatedBy,
						Created = row.Created
					});
				}

				return result;
			}
		}

		public void Create(string pageContentUniqName, string createdBy)
		{
			this.PageContentUniqName = pageContentUniqName;
			this.CreatedBy = createdBy;
			using (DB.IntellectShopDataContext dataContext = new DB.IntellectShopDataContext(Common.Globals.ConnectionString))
			{
				var pageContentDB = new DB.PageContent
				{
					PageContentUniqName = this.PageContentUniqName,
					Header = this.Header,
					Body = this.Body,
					CreatedBy = this.CreatedBy,
					Created = DateTime.Now,
				};
				dataContext.PageContents.InsertOnSubmit(pageContentDB);
				dataContext.SubmitChanges();
			}
		}

		public void Update(string updatedBy)
		{
			using (DB.IntellectShopDataContext dataContext = new DB.IntellectShopDataContext(Common.Globals.ConnectionString))
			{
				var pageContentDB = (
									from pageContent in dataContext.PageContents
									where pageContent.PageContentID == this.PageContentID
									select pageContent).SingleOrDefault();
				pageContentDB.PageContentUniqName = this.PageContentUniqName;
				pageContentDB.Header = this.Header;
				pageContentDB.Body = this.Body;
				pageContentDB.Updated = DateTime.Now;
				pageContentDB.UpdatedBy = updatedBy;

				dataContext.SubmitChanges();
			}
		}

		public static void Delete(int pageContentID)
		{
			using (DB.IntellectShopDataContext dataContext = new DB.IntellectShopDataContext(Common.Globals.ConnectionString))
			{
				var pageContentDB = (
									from pageContent in dataContext.PageContents
									where pageContent.PageContentID == pageContentID
									select pageContent).SingleOrDefault();
				dataContext.PageContents.DeleteOnSubmit(pageContentDB);
				dataContext.SubmitChanges();
			}
		}
	}
}
